package javalearning.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class ThreadTest {
    static int i = 0;

    public static void main(String[] args) {
        List<Future<String>> list = new ArrayList<>();
        System.out.println("ThreadTest begins");
        int i = Runtime.getRuntime().availableProcessors();
        System.out.println(i);
        ExecutorService pool = Executors.newFixedThreadPool(10);

        for (int i1 = 0; i1 < 10; i1++) {
            MYCallThread task = new MYCallThread();
            list.add(pool.submit(task));
        }
        for (Future<String> future : list) {
            try {
                System.out.println(future.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }
        pool.shutdown();
    }

    private static class MYCallThread implements Callable<String> {
        @Override
        public String call() throws Exception {
            return "测试" + ++i;
        }
    }
    /*
     List<Future<String>> list = new ArrayList<Future<String>>();//Future<String>是线程池执行后产生的结果类型，通过该类型的对象可以获取线程的返回的值
        ExecutorService pool = Executors.newFixedThreadPool(10);//线程池，产生10个线程备用
        for (int i = 0; i < 10; i++) {
            MYCallThread ct = new MYCallThread();
            list.add(pool.submit(ct));
        }
        for (Future<String> f : list) {
            try {
                System.out.println(f.get());
            } catch (InterruptedException | ExecutionException e) {
        System.out.println("在将线程结果带出时出错");
        }
        }
        pool.shutdownNow();
     */
}
